import 'package:ac/widget/ac_app_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';

import 'ac_web_view_logic.dart';

class AcWebViewPage extends StatefulWidget {
  const AcWebViewPage({super.key});

  @override
  State<AcWebViewPage> createState() => _AcWebViewPageState();
}

class _AcWebViewPageState extends State<AcWebViewPage> {
  final logic = Get.find<AcWebViewLogic>();

  final state = Get.find<AcWebViewLogic>().state;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    EasyLoading.show(status: 'loading...', maskType: EasyLoadingMaskType.black);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AcAppbar(
        title: Get.parameters['title'] ?? '',
      ),
      body: Column(
        children: [
          Expanded(child: _webview()),
        ],
      ),
    );
  }
  _webview() {
    return InAppWebView(
      initialUrlRequest: URLRequest(
        url: Uri.parse(
          Get.parameters['url'] ?? '',
        ),
      ),
      onReceivedServerTrustAuthRequest: (controller, challenge) async {
        return ServerTrustAuthResponse(
            action: ServerTrustAuthResponseAction.PROCEED);
      },
      onLoadStart: (controller, url) {
        debugPrint('onLoadStart: $url');
      },
      onLoadStop: (controller, url) {
        debugPrint('onLoadStop: $url');
        EasyLoading.dismiss();
      },
      onLoadError: (controller, url, code, message) {
        debugPrint('onLoadError: $url -- code: $code -- message: $message');
        if (EasyLoading.isShow) {
          EasyLoading.dismiss();
        }
      },
    );
  }

}
